package com.jk.controller;

import com.jk.service.DealInfoService;
import com.jk.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("dealinfo")
public class DealinfoController {
    @Autowired
    private DealInfoService dealInfoService;

    //根据条件进行分页查询
    @GetMapping("getDealInfo")
    public Map getDealInfo(int page, int rows, DealInfo dealInfo,HttpSession session) {
        //UserInfo user = session.getAttribute("logUser");
        return dealInfoService.getDealInfo(page, rows, dealInfo);
    }

    //根据时间倒序查询前七条数据
    @GetMapping("getInfo")
    public List<DealInfo> getInfo() {
        return dealInfoService.getInfo();
    }

    //三表联查账户管理中的相应数据
    @GetMapping("getAccountUser")
    public List<Map> getAccountUser(HttpSession session) {
        // User user = session.getAttribute("logUser");
        List<Map> map = dealInfoService.getAccountUser("4a885027e5f311e7ad7d00163e32bd1a");
        return map;
    }

    //查询该用户所拥有的银行卡
    @GetMapping("getBank")
    public List<Map> getBank(HttpSession session) {
        // User user = session.getAttribute("logUser");
        List<Map> list = dealInfoService.getBank("4a885027e5f311e7ad7d00163e32bd1a");
        return list;
    }

    //充值中带值进行下一步转到支付页面并新增一条数据
    @PostMapping("postStatus")
    public DealInfo postStatus(DealInfo dealInfo,HttpSession session) {
        dealInfo.setDealId(Random.RandomUUID());
        dealInfo.setDealTime(Random.nowDate());
        dealInfo.setDealObject("单次转入");
        dealInfo.setDealMoney(dealInfo.getDealMoney());
        dealInfo.setDealStatus(2);
        dealInfo.setRecordType(1);
        dealInfo.setDeleteStatus(1);
        dealInfo.setDealSerianumber(Random.serialNumber());
        //UserInfo userInfo = session.getAttribute("logUser");
        //dealInfo.setUserId(userInfo);
        dealInfoService.addDeal(dealInfo);
        dealInfo.setDealId(dealInfo.getDealId());
        dealInfo.setDealMoney(dealInfo.getDealMoney());
        return dealInfo;
    }

    //充值
    @PutMapping("putAccountMoney")
    public int putMoney(DealInfo dealInfo, UserAccount userAccount, AccountBank accountBank) {
        //判断支付密码是否正确
        int count = dealInfoService.getPayPwd(userAccount);
        if (count == 1) {
            dealInfo.setDealTime(Random.nowDate());
            dealInfo.setDealStatus(1);
            //将交易状态修改为交易成功
            dealInfoService.putDeal(dealInfo);
            //对账户金额进行相应修改
            //UserInfo countId = session.getAttribute("logUser");
             userAccount.setAccountId("4a885027e5f311e7ad7d00163e32bd1a");
            System.out.println(userAccount.getAccountAmount());
            dealInfoService.putAccountMoney(userAccount);
            dealInfo.setDealId(Random.RandomUUID());
            dealInfo.setDealTime(Random.nowDate());
            dealInfo.setDealObject("单次转入");
            dealInfo.setDealMoney(dealInfo.getDealMoney());
            dealInfo.setDealStatus(1);
            dealInfo.setRecordType(2);
            dealInfo.setDealFundflow(1);
            dealInfo.setDeleteStatus(1);
            //UserInfo userInfo = session.getAttribute("logUser");
            //dealInfo.setUserId(userInfo);
            dealInfoService.addDeal(dealInfo);

            //对对应银行卡金额进行相应修改
            dealInfoService.putBankMoney(accountBank);
            return 1;
        } else {
            return 2;
        }
    }

    //提现
    @PutMapping("putBankMoney")
    public int putMoney(UserAccount userAccount, AccountBank accountBank, DealInfo dealInfo) {
        int count = dealInfoService.getPayPwd(userAccount);
        if (count == 1) {
            dealInfo.setDealId(Random.RandomUUID());
            dealInfo.setDealTime(Random.nowDate());
            dealInfo.setDealObject("单次转入");
            dealInfo.setDealMoney(dealInfo.getDealMoney());
            dealInfo.setDealStatus(2);
            dealInfo.setRecordType(2);
            dealInfo.setDealFundflow(2);
            dealInfo.setDeleteStatus(1);
            //UserInfo userInfoSeesion = session.getAttribute("logUser");
            //dealInfo.setUserId(userInfo);
            dealInfoService.addDeal(dealInfo);
            //对账户金额进行增加修改
            userAccount.setAccountId("4a885027e5f311e7ad7d00163e32bd1a");
            dealInfoService.putAccountMoneyInsert(userAccount);
            //对对应银行卡金额进行减少修改
            dealInfoService.putBankMoneyReduce(accountBank);
            return 1;
        } else {
            return 2;
        }
    }

    //转账到支付宝页面点击下一步操作
    @GetMapping("getTransferPay")
    public Map getTransferPay(UserInfo userInfo, UserAccount userAccount,DealInfo dealInfo) {
        Map map=new HashMap();
        if (userInfo.getUserEmail() != null) {
            //通过邮箱获取对象id
            UserInfo userIn = dealInfoService.getOtherEmail(userInfo);

            /*map.put("userAccoun",userAccount.getAccountAmount());
            map.put("dealInf",dealInfo.getDealInfo());*/
            if(userIn!=null){
                map.put("userIn",userIn.getUserId());
                return map;
            }else {
                map.put("userIn","1");
                return map;
            }
        }
        if (userInfo.getUserNumber() != null) {
            //通过手机号获取对象id
            UserInfo userIn = dealInfoService.getOtherNumber(userInfo);

            /*map.put("userAccoun",userAccount.getAccountAmount());
            map.put("dealInf",dealInfo.getDealInfo());*/
            if(userIn!=null){
                map.put("userIn",userIn.getUserId());
                return map;
            }else {
                map.put("userIn","2");
                return map;
            }
        }
        return (Map) map.put("userIn",3);
    }

    //跳转到'转账到支付宝'中的支付页面查询被转账人真实姓名
    @GetMapping("getRealName")
    public AuthUserIdcard getRealName(UserInfo userInfo){
        //System.out.println(userInfo.getUserId());
        AuthUserIdcard authUserIdcard=dealInfoService.getRealName(userInfo);
        //System.out.println(authUserIdcard.getRealName());
        return authUserIdcard;
    }

    //转账到支付宝页面点击转账操作
    @GetMapping("getPwdverify")
    public int getPwdverify(AuthUserIdcard authUserIdcard,UserInfo userInfo,DealInfo dealInfo, UserAccount userAccount,HttpSession session){
        //判断支付密码是否正确
        int count = dealInfoService.getPayPwd(userAccount);
        if (count == 1) {

            //对转账账户金额进行相应减少修改
            //UserInfo logUser = session.getAttribute("logUser");
            //userAccount.setUserId(logUser.getUserId);
            userAccount.setAccountId("4a885027e5f311e7ad7d00163e32bd1a");
            dealInfoService.putSelfAccount(userAccount);

            //新增一条个人转账记录
            dealInfo.setDealId(Random.RandomUUID());
            dealInfo.setDealStatus(1);
            dealInfo.setDealMoney(userAccount.getAccountAmount());
            dealInfo.setDealTime(Random.nowDate());
            dealInfo.setDealObject(authUserIdcard.getRealName());
            dealInfo.setDealSerianumber(Random.serialNumber());
            dealInfo.setDealInfo(dealInfo.getDealInfo());
            dealInfo.setDealFundflow(1);
            dealInfo.setDealType(3);
            dealInfo.setDeleteStatus(1);
            //UserInfo userInfoSeesion = session.getAttribute("logUser");
            //dealInfo.setUserId(userInfo);
            dealInfoService.addDeal(dealInfo);

            //对被转账账户金额进行相应增加修改
            dealInfoService.putOtherAccount(userInfo,userAccount);
            //新增一条他人转账记录
            dealInfo.setDealId(Random.RandomUUID());
            dealInfo.setDealStatus(1);
            dealInfo.setDealMoney(userAccount.getAccountAmount());
            dealInfo.setDealTime(Random.nowDate());
            dealInfo.setDealObject(authUserIdcard.getRealName());
            dealInfo.setDealSerianumber(Random.serialNumber());
            dealInfo.setUserId(userInfo.getUserId());
            dealInfo.setDealInfo(dealInfo.getDealInfo());
            dealInfo.setDealFundflow(2);
            dealInfo.setDealType(3);
            dealInfo.setDeleteStatus(1);
            dealInfoService.addDeal(dealInfo);
            return 1;
        }
        return 2;
    }

    //在转账到银行卡页面根据银行卡号查询相对持卡人和卡名
    @GetMapping("getAccountBank")
    public AccountBank getAccountBank(AccountBank accountBank){
        AccountBank account = dealInfoService.getAccountBank(accountBank);
        return account;
    }

    //带值跳转到转账到银行卡的确认信息页面
    /*public ModelAndView toConfirmMessage(AccountBank accountBank,DealInfo dealInfo){
        ModelAndView mv=new ModelAndView();
        mv.addObject("accountBank",accountBank);
        mv.addObject("dealInfo",dealInfo);
        mv.setViewName();
        return mv;
    }*/

    //点击确认密码并支付带值跳转到支付页面
     /*public ModelAndView toPayPage(AccountBank accountBank,DealInfo dealInfo){
        ModelAndView mv=new ModelAndView();
        mv.addObject("accountBank",accountBank);
        mv.addObject("dealInfo",dealInfo);
        mv.setViewName();
        return mv;
    }*/

    //点击转账进行一系列的转账操作以及新增日志操作
    @PutMapping("putMoney")
    public int putMoney(AuthUserIdcard authUserIdcard,AccountBank accountBank,DealInfo dealInfo,UserAccount userAccount){
        //判断支付密码是否正确
        int count = dealInfoService.getPayPwd(userAccount);
        if (count == 1) {

            //对转账账户金额进行相应减少修改
            //UserInfo logUser = session.getAttribute("logUser");
            //userAccount.setUserId(logUser.getUserId);
            userAccount.setAccountId("4a885027e5f311e7ad7d00163e32bd1a");
            dealInfoService.putSelfAccount(userAccount);
            //新增一条个人转账记录
            dealInfo.setDealId(Random.RandomUUID());
            dealInfo.setDealStatus(1);
            dealInfo.setDealMoney(userAccount.getAccountAmount());
            dealInfo.setDealTime(Random.nowDate());
            dealInfo.setDealObject(authUserIdcard.getRealName());
            dealInfo.setDealSerianumber(Random.serialNumber());
            dealInfo.setDealInfo(dealInfo.getDealInfo());
            dealInfo.setDealFundflow(1);
            dealInfo.setDealType(3);
            dealInfo.setDeleteStatus(1);
            //UserInfo userInfoSeesion = session.getAttribute("logUser");
            //dealInfo.setUserId(userInfo);
            dealInfoService.addDeal(dealInfo);

            //对被转账银行卡金额进行相应增加修改
            dealInfoService.putOtherBank(accountBank,userAccount);
            return 1;
        }
        return 2;
    }

    //逻辑删除（修改状态放入回收站中）
    @PutMapping("putDeleteStatus")
    public void putDeleteStatus(DealInfo dealInfo){
        dealInfoService.putDeleteStatus(dealInfo);
    }

    //查询当前用户账户余额
    @GetMapping("getUserAccountMoney")
    public List<Map> getUserAccountMoney(){
        return dealInfoService.getUserAccountMoney("4a885027e5f311e7ad7d00163e32bd1a");
    }

    //查询当前账户的充值记录
    @GetMapping("getTopUpInfo")
    public List<DealInfo> getTopUpInfo(){
        return dealInfoService.getTopUpInfo("4a885027e5f311e7ad7d00163e32bd1a");
    }

    //查询当前账户的提现记录
    @GetMapping("getDepositInfo")
    public List<DealInfo> getDepositInfo(){
        return dealInfoService.getDepositInfo("4a885027e5f311e7ad7d00163e32bd1a");
    }

    //回收站批量删除
    @DeleteMapping("deleteBatch")
    public Map deleteBatch( List<String> ids){
        System.out.println(ids);
        Map map=new HashMap();
       // int count=dealInfoService.deleteBatch(ids);
        /*if(count>1){
            map.put("flag",1);
        }else{
            map.put("flag",2);
        }*/
        return map;
    }


    @GetMapping("getTopUpInfo2")
    public List<DealInfo> getTopUpInfo2(int status){
        System.out.println(status);
        return dealInfoService.getTopUpInfo2("4at885027e5f311e7ad7d00163e32bd1a",status);
    }

    //回收站展示
    @GetMapping("getRecycle")
    public List<DealInfo> getRecycle(){
        return dealInfoService.getRecycle();
    }

    //账户设置修改手机号
    @PutMapping("putNumber")
    public void putNumber(String number,String pnumber){
        dealInfoService.putNumber(number,pnumber);
    }

    //账户设置中修改邮箱
    @PutMapping("putEmail")
    public void putEmail(String email,String pemail){
        dealInfoService.putEmail(email,pemail);
    }
}


